/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package db.JDBC.core;

import core.*;
import core.comparators.ComparatorFactory;
import core.comparators.ProgramComparator.PROGRAMCOMPARATORTYPE;
import db.JDBC.*;
import db.core.interfaces.ProgramDAOCore;
import db.interfaces.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.TreeSet;
/**
 *
 * @author Anton
 */
public class ProgramJdbcDAOCore implements ProgramDAOCore
{
    private static ProgramJdbcDAOCore instance = null;
    private ProgramDAO programDAO;
    private CRUDJdbcDAOCore crud;

    /**
     * 
     */
    private ProgramJdbcDAOCore()
    {
        JdbcDaoFactory daoFactory = JdbcDaoFactory.getInstance();
        programDAO = daoFactory.getProgramJdbcDao();
        crud = CRUDJdbcDAOCore.getInstance();
    }

    /**
     * 
     * @return
     */
    public static ProgramJdbcDAOCore getInstance()
    {
        if(instance == null)
            instance = new ProgramJdbcDAOCore();
        return instance;
    }

    /**
     * 
     * @param id
     * @return
     */
    public Program getProgramById(long id)
    {
        return crud.getProgram(id);
    }

    /**
     *
     * @param beginsWith
     * @param maxResult
     * @return
     */
    public Collection<String> searchForStringPrograms(String beginsWith, int maxResult)
    {
        Collection<Program> names  = programDAO.searchForProgramsByName(beginsWith);
        Collection<Program> courseIds  = programDAO.searchForProgramsByProgramId(beginsWith);

        Collection<String> t = new TreeSet<String>();
        List<String> returnCollection = new ArrayList<String>();
        if(names != null)
            for(Program p : names)  
                if(p != null)
                    t.add(p.getName());
        if(courseIds != null)
            for(Program p : courseIds)
                if(p != null)
                    t.add(p.getProgramId());

        for(String s : t)
            if(s != null)
                returnCollection.add(s);

        if(returnCollection.size() < maxResult)
            maxResult = returnCollection.size();
        return returnCollection.subList(0, maxResult);
    }
    /**
     *
     * @param beginsWith
     * @return
     */
    public Collection<Program> searchForObjectPrograms(String beginsWith)
    {
        Collection<Program> names  = programDAO.searchForProgramsByName(beginsWith);
        Collection<Program> courseIds  = programDAO.searchForProgramsByProgramId(beginsWith);
        
        Collection<Program> t = new TreeSet<Program>(ComparatorFactory.getProgramComparator(PROGRAMCOMPARATORTYPE.ID, true));
        List<Program> returnCollection = new ArrayList<Program>();
        
        if(names != null)
            for(Program p : names)
                if(p != null)
                    t.add(p);

        if(courseIds != null)
            for(Program p : courseIds)
                if(p != null)
                    t.add(p);
        

        for(Program p : t)
            if(p != null)
                returnCollection.add(p);
        Collections.sort(returnCollection, ComparatorFactory.getProgramComparator(PROGRAMCOMPARATORTYPE.NAME, true));

        return returnCollection;
    }

    /**
     * 
     * @param name
     * @return
     */
    public Collection<Program> getProgramsByName(String name)
    {
        if(name == null)
            return null;
        Collection<Program> programs = new ArrayList<Program>();
        for(Program p : programDAO.getProgramsByName(name))
            if(p != null)
                programs.add(crud.getProgram(p.getId()));
        return programs;
    }
    /**
     * 
     * @param programId
     * @return
     */
    public Collection<Program> getProgramsByProgramId(String programId)
    {
        if(programId == null)
            return null;
        Collection<Program> programs = new ArrayList<Program>();
        for(Program p : programDAO.getProgramsByProgramId(programId))
            if(p != null)
                programs.add(crud.getProgram(p.getId()));
        return programs;
    }
    /**
     * 
     * @return
     */
    public Collection<Program> getAllPrograms()
    {
        Collection<Program> programs = new ArrayList<Program>();
        for(Program p : programDAO.getAllPrograms())
            if(p != null)
                programs.add(crud.getProgram(p.getId()));
        return programs;
    }

    /**
     * 
     * @param program
     * @param course
     */
    public void addCourseToProgram(Program program, Course course)
    {
        if(program == null || course == null)
            return;
        crud.addCourseToProgram(program, course);
    }
    /**
     * 
     * @param program
     * @param course
     */
    public void deleteCourseFromProgram(Program program, Course course)
    {
        if(program == null || course == null)
            return;
        crud.deleteCourseFromProgram(program, course);
    }

    /**
     * 
     * @param program
     */
    public void addProgram(Program program)
    {
        if(program == null)
            return;
        crud.addProgram(program);
    }
    /**
     * 
     * @param program
     */
    public void updateProgram(Program program)
    {
        if(program == null)
            return;
        crud.updateProgram(program);
    }
    /**
     * 
     * @param program
     */
    public void deleteProgram(Program program)
    {
        if(program == null)
            return;
        crud.deleteProgram(program);        
    }
}
